﻿/**
 *		导出库中所选的图片文件, 默认用其在库中的名称.
 *		tamt 	2009/09/2	ver 0.9
 *		tamt	2009/11/16	ver 1.0	添加了文件名称的判断.
 */
var selItems = fl.getDocumentDOM().library.getSelectedItems(); 
if(selItems.length == 0){
	alert('请先选择一个图片.');
}else{

	//-------------界面文件的生成----------------------
	var tmpXmlFile = fl.configURI + "/tmp.xml";
	var xmlGui = buildXulGui();
	FLfile.write(tmpXmlFile, xmlGui);
	var setting = fl.getDocumentDOM().xmlPanel(tmpXmlFile);
	//-------------------------------------------------
	var folderPath = setting.folder;
	for(var i = 0; i<selItems.length; i++){
		if(selItems[i].itemType == 'bitmap'){
			var n_str = selItems[i].name;
			var filePath = folderPath + '/' + n_str.substring(n_str.lastIndexOf('/')<0?0:(n_str.lastIndexOf('/') + 1));
			
			var fileEx = selItems[i].sourceFilePath.substring(selItems[i].sourceFilePath.lastIndexOf('.'));
			if(fileEx.length == 4 || fileEx.length == 5){
				if(n_str.indexOf(fileEx)!=filePath.length - fileEx.length)filePath += fileEx;
			}
			
			selItems[i].exportToFile(filePath);
		}
	}
	alert('the files are exported.');
}

function buildXulGui()
{
	var t = fl.getDocumentDOM().pathURI;
	var folderPath = t.substr(0, t.lastIndexOf('/'));
  return '<dialog id="dialog" title="导出库中的所选图片" buttons="accept, cancel"> \
      \
      <script> \
      function browseFolder() \
      { \
        var folderURL = fl.browseForFolderURL(); \
        if(folderURL != null) \
        { \
          fl.xmlui.set("folder", folderURL); \
        } \
      } \
      </script> \
      \
     <vbox> \
      <label width="300" value="选择要存放的文件夹路径"/> \
      <textbox value="' + folderPath +'" width="300" id="folder"/> \
      <button id="openFolder" label="选择..." oncommand="browseFolder()" disabled="true"/>	\
    </vbox> \
    \
  </dialog>';
}